package cn.bopomo.dealer.base.action.ajax;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.vo.AuGroup;
import cn.bopomo.dealer.base.vo.AuthorizeList;

public class LoadGroupsByMidAjax extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		String moduleId = request.getParameter("mid");
		
		
		List groupList = os.getGroupListByModule(new Integer(moduleId));
		
		if (groupList != null && groupList.size()>0){
			JSONArray  array = this.toJsonArray(groupList);
			JSONObject json = new JSONObject();
			json.put("groups", array);
			response.getWriter().write(json.toString());
			return null;
		}else{
			response.getWriter().write("");
			return null;
		}
		
	}

	
	protected JSONArray toJsonArray(List objectList) {
		JSONArray  array = new JSONArray();
		for (int i = 0;i<objectList.size();i++){
			AuGroup tmp = (AuGroup)objectList.get(i);
			JSONObject json = new JSONObject();
			json.put("groupId",tmp.getId().toString());
			json.put("groupName", tmp.getGroupName());
			array.put(json);
		}
		return array;
	}
}
